package com.qyhl.webtv.module_news.news.union.town.addmeeting;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by qyhl on 2018/3/6.
 */

public class AddMeetingModel implements AddMeetingContract.AddMeetingModel {
    private AddMeetingPresenter mPresenter;

    public AddMeetingModel(AddMeetingPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * @param streetId
     * @param content
     * @param type     1 文字 2 图文
     * @param images
     */
    @Override
    public void addMeeting(String streetId, String content, String type, String images) {
        String nickName = CommonUtils.getInstance().getNickName();
        String headicon =CommonUtils.getInstance().getUserLogo();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("streetId", streetId);
        hashMap.put("content", content);
        hashMap.put("type", type);
        String userName = CommonUtils.getInstance().getUserName();
        hashMap.put("userName", userName);
        hashMap.put("nickName", nickName);
        hashMap.put("userAvatar", headicon);
        if ("2".equals(type)) {
            hashMap.put("images", images);
        }

        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = NewsUrl.GET_MEETING_ADD + "?timestamp=" + time;
        StringBuffer mContent = new StringBuffer();
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            mContent = mContent.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String content1 = mContent.toString().substring(0, mContent.length() - 1);

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, content1))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.addError("网络异常，提交失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.addSuccess("提交成功！");
                        } else {
                            mPresenter.addError("解析异常，提交失败！");
                        }
                    }
                });

    }

    @Override
    public void getToken(final boolean isUpload) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = NewsUrl.CIRCLE_URL + "?timestamp=" + time;
        
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });
    }
}
